package cheeese.and.crackers;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
import android.widget.TextView;

public class IndividualTaskViewActivity extends Activity{
	
	private UserList users;
	private User activeUser;
	private Task activeTask;
	private Button backButton;
	private CheckBox completeBox;
	
	/** 
	 * Called when the activity is first created. 
     * 
     * @param savedInstanceSate Saved state of activity when last suspended
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.individualtaskviewscreen); 
        backButton = (Button)findViewById(R.id.buttonBack);
        
        IndividualTaskViewClickListener myListener = new IndividualTaskViewClickListener();
        
        backButton.setOnClickListener(myListener);
        ((TextView)findViewById(R.id.textLocation)).setOnClickListener(myListener);
        completeBox = (CheckBox)findViewById(R.id.checkBoxComplete);
        
    }
     
     /**
      * Method to call when activity is resumed
      */
     @Override
     public void onResume(){
    	 super.onResume();
    	 users = new UserList();
         try {
 			users.load(getApplicationContext());
 		} catch (Exception e) {
 			Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
     		try {
     			Thread.sleep(2000);
     		} 
     		catch(InterruptedException e2) {} 
     		android.os.Process.killProcess(android.os.Process.myPid());
 		}
    	//Get Active User
 		Bundle activeUserBundle = this.getIntent().getExtras();
 		String activeUserString = activeUserBundle.getString("ACTIVE_USER");
 		activeUser = users.getUser(activeUserString);
 		String activeTaskString = activeUserBundle.getString("ACTIVE_TASK");
 		activeTask = activeUser.getTasks().getTask(activeTaskString);
 		
 		((TextView)findViewById(R.id.textDisplayDescription)).setText(activeTask.getDisplayDescription());
 		((TextView)findViewById(R.id.textLocation)).setText(activeTask.getLocation());
 		((TextView)findViewById(R.id.textDate)).setText(activeTask.getDueDate());
 		((TextView)findViewById(R.id.textDetails)).setText(activeTask.getDetails());
 		((TextView)findViewById(R.id.textCategory)).setText(activeTask.getType());
 		
 		completeBox.setChecked(activeTask.getComplete());
     }
     
     /**
      * Method to call when activity is paused
      */
      @Override
      public void onPause(){
  	   super.onPause();
  	   activeTask.setComplete(completeBox.isChecked());
      	try{
      		users.save(getApplicationContext());
      	}
      	catch(Exception e){
      		Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
      		try {
      			Thread.sleep(2000);
      		} 
      		catch(InterruptedException e2) {} 
      		android.os.Process.killProcess(android.os.Process.myPid());
      	}
      }
      
      /**
       * Listener for button
       */
      public class IndividualTaskViewClickListener implements OnClickListener {
      	@Override
      	/**
      	 * Checks which button was pressed and takes appropriate action, login or create user
      	 * @param v The view which was clicked
      	 */
  		public void onClick(View v) {
      		if(v == findViewById(R.id.textLocation)){
      			Intent mapViewIntent = new Intent(v.getContext(),MapViewActivity.class);
      			mapViewIntent.putExtra("ACTIVE_USER", activeUser.getLoginId());
    			mapViewIntent.putExtra("ACTIVE_TASK", activeTask.getDisplayDescription());
      			startActivity(mapViewIntent);
      		}
      		else{
      			finish();
      		}
      	}
      }
}
